# 每日定时启动客户端
# import schedule
import pandas as pd
import os,subprocess,time
import pyautogui



beginPath=os.path.split(os.path.realpath(__file__))[0]
fullPath=os.path.join(beginPath,"autoStart.csv")

setPath=r"C:\Users\AXZQ\Desktop\AUTO\AUTO_Start\autoStart.csv"
configData=pd.read_csv(setPath)
configDataTrue=configData[configData["是否启动"]=="Y"]
start_time =configDataTrue["启动时间(需要同一个时间)"].iloc[0]
indexconfig=len(configDataTrue)
# time.sleep(3)
def start_client():
	for i in range(indexconfig):
		client=configDataTrue["系统"].iloc[i]
		clientPath=configDataTrue["客户端exe目录"].iloc[i]
		userName=configDataTrue["账号"].iloc[i]
		password=configDataTrue["密码"].iloc[i]
		processName=configDataTrue["进程名称"].iloc[i]
		exeNo = os.popen('tasklist /FI "IMAGENAME eq %s"' % processName).read().count(processName)   #判断存在的进程数量
		print(f"程序{client}，进程数：{exeNo}")
		if exeNo==1:
			# 如果存活则杀死进程
			os.system("taskkill /f /im " + processName)
			print(f"程序{client}已杀掉进程")
			# 重新打开客户端
			# subprocess.Popen(clientPath, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
			print(clientPath)

			pyautogui.press('win')  # 模拟按下Win键，在搜索框中输入"cmd"
			pyautogui.write('cmd')  # 输入"cmd"，启动命令行
			pyautogui.press('enter')  # 模拟按下回车键
			pyautogui.write('cd D:\Ax\OQ联测\安信OneQuant_管理端2.5.0.1新协议')  # 进入Exe文件所在目录
			pyautogui.press('enter')
			pyautogui.write('OneQuantManager.exe')  # 启动目标Exe文件
			pyautogui.press('enter')
			# os.startfile(clientPath)
			print(f"程序{client}已启动")
			time.sleep(5)
		else:
			# 重新打开客户端
			print(clientPath)
			# subprocess.Popen(clientPath, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE)
			os.startfile(clientPath)
			print(f"程序{client}已启动")
			time.sleep(5)
		if client=="OneQuant":
			print("开始操作")
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(5)
		elif client=="手工T0":
			print("开始操作")
			pyautogui.press('tab')
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(5)
		elif client=="Ptrade":
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(30)
		elif client=="恒生PB":
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(30)
		elif client=="络町PB":
			pyautogui.press('tab')
			pyautogui.press('tab')
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(30)
		elif client=="迅投PB":
			pyautogui.typewrite(userName)
			pyautogui.press('tab')
			pyautogui.typewrite(password)
			pyautogui.press('enter')
			time.sleep(30)
start_client()
# # 定时轮询启动任务
# schedule.every().day.at('22:11').do(start_client)
# while True:
# 	schedule.run_pending()
# 	time.sleep(20)
